#!/usr/bin/env tclsh
source streamtestlib.tcl

# Define records, protocol and startup (text goes to files)
# The asynPort "device" is connected to a network TCP socket
# Talk to the socket with send/receive/assure
# Send commands to the ioc shell with ioccmd

set records {
    record (ao, "DZ:test1")
    {
        field (DTYP, "stream")
        field (OUT,  "@test.proto test1 device")
    }
    record (ai, "DZ:test2")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test2 device")
    }
    record (ai, "DZ:test3")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test3 device")
    }
    record (ai, "DZ:test4")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test4 device")
    }
    record (ai, "DZ:test5")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test5 device")
    }
    record (ai, "DZ:test6")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test6 device")
    }
    record (ai, "DZ:test7")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto test7 device")
    }
}

set protocol {
    Terminator = LF;
    test1 {out "%T(%d.%m.%Y %H:%M:%.2S %z)"; }
    test2 {in "%T"; out "%.0f %T"; }
    test3 {in "%(TIME)T(%Ed. %B %Y %H:%M:%.S) %f"; out "%(TIME)T(%d.%m.%Y %H:%M:%.2S %z)"; }
    test4 {in "%(TIME)T(%c) %f"; out "%(TIME)T(%a %d.%m.%Y %H:%M:%.2S %z)"; }
    test5 {in "%T(%H %p)"; out "%T(%H)"; }
    test6 {in "%T(%p %H)"; out "%T(%H)"; }
    test7 {in "%T(%d.%m.%Y %T %z)"; out "%T(%d.%m.%Y %T %z) %.6f"; }
}

set startup {
}

set debug 0

package require Epics
proc checkTS {pv timestamp} {
    global faults
    pvget $pv
    array set PV [pvinfo $pv]
    if {$PV(TIME) != $timestamp} {
        puts "expected timestamp: $timestamp"
        puts "received timestamp: $PV(TIME)"
        incr faults
    }
}

startioc

put DZ:test1 1044068706.789
assure "01.02.2003 04:05:06.79 +0100\n"

put DZ:test1 1057025106.789
assure "01.07.2003 04:05:06.79 +0200\n"

process DZ:test2
send "2003-02-01 04:05:06\n"
assure "1044068706 2003-02-01 04:05:06\n";

process DZ:test3
send "1. February 2003 04:05:06.789 3.1415\n"
assure "01.02.2003 04:05:06.79 +0100\n";
checkTS DZ:test3 "02/01/03 04:05:06.789000000"

process DZ:test3
send "1. July 2003 04:05:06.789123 3.1415\n"
assure "01.07.2003 04:05:06.79 +0200\n";
checkTS DZ:test3 "07/01/03 04:05:06.789123000"

process DZ:test4
send "mon jan 2 04:05:06 2003 3.1415\n"
assure "Thu 02.01.2003 04:05:06.00 +0100\n";
checkTS DZ:test4 "01/02/03 04:05:06.000000000"

process DZ:test5
send "12 a\n"
assure "00\n";
process DZ:test5
send "01 a\n"
assure "01\n";
process DZ:test5
send "2 a\n"
assure "02\n";
process DZ:test5
send "11 a\n"
assure "11\n";
process DZ:test5
send "12 p\n"
assure "12\n";
process DZ:test5
send "01 p\n"
assure "13\n";
process DZ:test5
send "11 p\n"
assure "23\n";

process DZ:test6
send "am 12\n"
assure "00\n";
process DZ:test6
send "Am 01\n"
assure "01\n";
process DZ:test6
send "aM 2\n"
assure "02\n";
process DZ:test6
send "AM 11\n"
assure "11\n";
process DZ:test6
send "p 12\n"
assure "12\n";
process DZ:test6
send "P 01\n"
assure "13\n";
process DZ:test6
send "PM 11\n"
assure "23\n";

process DZ:test7
send "1.2.2010 12:56:32 +0000\n"
assure "01.02.2010 13:56:32 +0100 1265028992.000000\n";
process DZ:test7
send "1.7.2010 12:56:32 +0000\n"
assure "01.07.2010 14:56:32 +0200 1277988992.000000\n";
finish
